package com.kfm.pm.stream;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Demo01 {

    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        Collections.addAll(names, "张三丰", "张无忌", "周芷若", "赵敏", "张强");
        System.out.println(names);
        // 找到所有姓张的 并且 三个字 放到一个新的集合
        List<String> threeName = new ArrayList<>();
        names.forEach(s -> {
            if (s.startsWith("张") && s.length() == 3) {
                threeName.add(s);
            }
        });
        System.out.println(threeName);

        HashSet<String> hashSet = new HashSet<>();
        // List\set  stream()  Map   entrySet().stream() 键值对的一个流  数组 Arrays.stream(arr) 获取arr对应的流
        Stream<String> stream = names.stream();
        Stream<String> stream1 = hashSet.stream();
        HashMap<String, Integer> hashMap = new HashMap<>();
//        hashMap
        Stream<Map.Entry<String, Integer>> stream2 = hashMap.entrySet().stream();
        String[] strings = new String[]{"1", "2", "3"};
        Stream<String> stream3 = Arrays.stream(strings);
        Stream<String> zhang = stream.filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                // s stream 流 里面的元素
                System.out.println(s);
                return s.startsWith("张") && s.length() == 3;
            }
        });
        //        List<String> collect = zhang.collect(Collectors.toList());
//        System.out.println(collect);
        // stream 是获取的流对象 filter 中间（添加了一个过滤条件） collect 终结方法 流式编程（链式编程）
        Set<String> set = stream.filter(s -> s.startsWith("张") && s.length() == 3).collect(Collectors.toSet());

        System.out.println(set);



    }
}
